% 信号提取分析接口测试
clc;
clear;


%bin_file_path = 'D:/Project/Data/YCS-牛长骨-1-PL-400-2023-10-20-06-46-00/2023-10-20-06-52-15/Rawdata_FPGA1_Frame0.bin';
% bin_file_path = 'D:\Project\Results\Knuckle\hanshuai-700-P4-2V-2023-08-09-17-09-38\2023-08-09-17-09-39/Rawdata_FPGA1_Frame0.bin';
% acq_parameter_file_path = "D:\Project\Results\Knuckle\hanshuai-700-P4-2V-2023-08-09-17-09-38\2023-08-09-17-09-39/acquisition_parameter.csv";
% save_path = 'D:/Project/Results/Knuckle/';

bin_file_path = 'D:\Project\Results\Knuckle\Boyi_20250816 指关节 PAUS_2.5MHz/Rawdata_FPGA1_Frame9.bin';
acq_parameter_file_path = "D:\Project\Results\Knuckle\Boyi_20250816 指关节 PAUS_2.5MHz/acquisition_parameter.csv";
save_path = 'D:/Project/Results/Knuckle/';

%img = reconDAS(path);
addpath(genpath('D:/Project/pabone\matlab_scripts/image_signal_analysis/MUST'));
addpath(genpath('D:/Project/pabone\matlab_scripts/image_signal_analysis'));

%% define the ultrasound probe.
%% 2.5MHZ 探头参数==================%
%% PA&US shared reconstruction parameters =========%
params.Nelements = 64;
params.fs = 40e6;   % [Hz]
params.fc = 2.5e6;  % [Hz]
params.bandwidth = 90;    % [percent]
params.pitch = 0.300e-3;  % pitch == spacing [mm]
params.width = 0.254e-3;  %  [mm]
params.radius = inf;   % 线阵
params.c = 1500;  %[m/s]
params.fnumber = [];


[Data_Rf, acqLen] = DataExtractFromBin(bin_file_path, 0);

Data_US_Rf = Data_Rf.US(1:4000,:,:); 
for i = 1:64
    subplot(8, 8, i); % 将图形窗口分为 8x8 的子图，方便显示 64 个信号
    plot(Data_US_Rf(i, :)); % 绘制第 i 维的信号
    title(['Signal ', num2str(i)]); % 设置子图标题
    xlabel('Sample Index'); % 设置 x 轴标签
    ylabel('Amplitude'); % 设置 y 轴标签
end

Data_PA_Rf = Data_Rf.PA(1:4000,:,:); 
for i = 1:64
    subplot(8, 8, i); % 将图形窗口分为 8x8 的子图，方便显示 64 个信号
    plot(Data_PA_Rf(i, :)); % 绘制第 i 维的信号
    title(['Signal ', num2str(i)]); % 设置子图标题
    xlabel('Sample Index'); % 设置 x 轴标签
    ylabel('Amplitude'); % 设置 y 轴标签
end

%% function [USPAI] = USPArecon(bin_file_path, acq_parameter_file_path, save_path, probe_param, modality, recon_method, log_compress_parameter, sound_speed)

us_image = USPArecon(bin_file_path, acq_parameter_file_path, save_path, params, 0, "DAS", 33, 1500);
pa_image = USPArecon(bin_file_path, acq_parameter_file_path, save_path, params, 1, "DAS", 32, 1500);

us_image.image(1:200,:)=0;
figure,imagesc(us_image.image);
colormap('gray');xlabel('[mm]'), ylabel('[mm]')
%figurepalette(image.Colormap);
set(gca, 'box', 'off',...
         'linewidth',4.5,...
         'TickDir','in',...
         'fontsize',16,'fontname','Times','FontWeight','bold','dataaspectratio',[1,1,1],'XTick', [1 96 192 288 384],'XTickLabel', [0 5 10 15 20],'YTick', [1 100 200],'YTickLabel', [25 30 35],'box','on')

filename = sprintf('D:/Project/Results/Knuckle/knuckle_us.png');
% 将 source.p0 的数据范围缩放到 [0, 255] 以便于保存为图像
usi_0 = mat2gray(us_image.image);    
% 使用 imwrite 函数保存图像
imwrite(uint8(usi_0 * 255), filename);




pa_image.image(1:200,:)=0;
figure,imagesc(pa_image.image);
colormap("hot");xlabel('[mm]'), ylabel('[mm]')
set(gca, 'box', 'off',...
         'linewidth',4.5,...
         'TickDir','in',...
         'fontsize',16,'fontname','Times','FontWeight','bold','dataaspectratio',[1,1,1],'XTick', [1 96 192 288 384],'XTickLabel', [0 5 10 15 20],'YTick', [1 100 200],'YTickLabel', [25 30 35],'box','on')

filename = sprintf('D:/Project/Results/Knuckle/knuckle_pa.png');
% 将 source.p0 的数据范围缩放到 [0, 255] 以便于保存为图像
ipa_0 = mat2gray(pa_image.image);    
% 使用 imwrite 函数保存图像
imwrite(uint8(ipa_0 * 255), filename);

